<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/6
 * Time: 15:29
 */

namespace Xcx\Controller;

use Xcx\Common\ErrorCode;
use Xcx\Model\AddressModel;

class AddressController extends BaseController
{
    protected $Address;
    public function __construct()
    {
        parent::__construct();
        $this->Address = new AddressModel();
    }

    /**
     * 添加地址
     */
    public function addAddress()
    {
        $name = I('name');
        $phone = I('phone');
        $province = I('province');
        $city = I('city');
        $area = I('area');
        $address = I('address');
        $isDefault = I('is_default',0);

        $res = $this->Address->addAddress($name, $phone, $province, $city, $area, $address, $isDefault);

        if ($res) {
            $arr = array('code'=>0);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'地址添加失败');
        }

        $this->ajaxReturn($arr);
    }

    /**
     * 更新地址
     */
    public function updateAddress()
    {
        $id = I('id');
        $name = I('name');
        $phone = I('phone');
        $province = I('province');
        $city = I('city');
        $area = I('area');
        $address = I('address');
        $isDefault = I('is_default',0);

        $res = $this->Address->updateAddress($id, $name, $phone, $province, $city, $area, $address, $isDefault);

        if ($res) {
            $arr = array('code'=>0);
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'地址更新失败');
        }

        $this->ajaxReturn($arr);
    }

    /**
     * 获取地址列表
     */
    public function getAddressList()
    {
        $list = $this->Address->getAddressList();
        $this->ajaxReturn(array('code'=>0,'list'=>$list));
    }

    /**
     * 删除地址
     */
    public function deleteAddress()
    {
        $id = I('id');
        $res = $this->Address->deleteAddress($id);
        if ($res) $arr = array('code'=>0);
        else $arr = array('code'=>ErrorCode::$Fail,'msg'=>'删除失败');
        $this->ajaxReturn($arr);
    }

    /**
     * 获取地址详情
     */
    public function getAddressInfo()
    {
        $id = I('id');
        $res = $this->Address->getAddressDetailsInfo($id);
        if ($res) $arr = array('code'=>0,'data'=>$res);
        else $arr = array('code'=>ErrorCode::$Fail,'msg'=>'删除失败');
        $this->ajaxReturn($arr);
    }

    /**
     * 获取默认地址
     */
    public function getDefaultAddress()
    {
        $address = $this->Address->getDefaultAddress();
        $this->ajaxReturn($address);
    }

}